home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 6 / MacMania 6.toast / / Tools&Utilities / TouchMe 1.2□ / touchMe 1.2 Folder / touchMe source codes / CW11 PP source / source / Common Lib / CAppleGuideFile.cp next >
Encoding:
Text File  |  1997-04-25  |  7.0 KB  |  282 lines  |  [TEXT/CWIE]

  1. // ==================================================
  2. //    CAppleGuideFile.cp
  3. //    Copyright (C) 1997 Mizutori Tetsuya
  4. //    March 21, 1997.
  5. // ==================================================
  6. //    All documents are pretty-printed in 10-point Geneva font.
  7.  
  8. #include <AppleGuide.h>
  9. #include <TextUtils.h>
  10.  
  11. #include <LString.h>
  12. #include <UEnvironment.h>
  13. #include <UDesktop.h>
  14.  
  15. #include "AGFile.h"
  16.  
  17. #include "CAppleGuideFile.h"
  18. #include "UFileTools.h"
  19. #include "UErrorMessage.h"
  20.  
  21.  
  22. const OSType    kOSTypeNil    = (Int32) -1;
  23.  
  24.  
  25. // --------------------------------------------------
  26. //        • CAppleGuideFile
  27. // --------------------------------------------------
  28.  
  29. CAppleGuideFile::CAppleGuideFile()
  30. {
  31.     mGotGuideFile = false;
  32.     mGuideCreatorType = kOSTypeNil;
  33.     mGuideFileRefNum = 0;
  34.     mGuideFilename[0] = 0;
  35. }
  36.  
  37.  
  38. // --------------------------------------------------
  39. //        • CAppleGuideFile()
  40. // --------------------------------------------------
  41.  
  42. CAppleGuideFile::CAppleGuideFile(
  43.     ConstStr255Param    inFileName )
  44. {
  45.     mGotGuideFile = false;
  46.     mGuideCreatorType = kOSTypeNil;
  47.     mGuideFileRefNum = 0;
  48.     LString::CopyPStr( inFileName, mGuideFilename );
  49. }
  50.  
  51.  
  52. // --------------------------------------------------
  53. //        • CAppleGuideFile()
  54. // --------------------------------------------------
  55.  
  56. CAppleGuideFile::CAppleGuideFile(
  57.     OSType            inCreatorType )
  58. {
  59.     mGotGuideFile = false;
  60.     mGuideCreatorType = (OSType) inCreatorType;
  61.     mGuideFileRefNum = 0;
  62.     mGuideFilename[0] = 0;
  63. }
  64.  
  65.  
  66. // --------------------------------------------------
  67. //        • ~CAppleGuideFile
  68. // --------------------------------------------------
  69.  
  70. CAppleGuideFile::~CAppleGuideFile()
  71. {
  72.     if ( ! AppleGuideIsPresent() ) return;
  73.  
  74.     AGErr    err;
  75.     // kAGIsActive(open), kAGIsSleeping(running but not open), kAGIsNotRunning(not running)
  76.     AGStatus    agStatus = ::AGGetStatus();
  77.  
  78.     switch ( agStatus ) {
  79.         case kAGIsNotRunning:
  80.             // Apple Guide is not running. So we do nothing to exit.
  81.             break;
  82.  
  83.         case kAGIsSleeping:
  84.             // Apple Guide is running, but no guide file is open.
  85.             err = ::AGQuit();
  86.             break;
  87.  
  88.         case kAGIsActive:
  89.             // Apple Guide is running, and some guide file is open.
  90.             if ( ! ::AGIsDatabaseOpen( mGuideFileRefNum ) ) break;
  91.             // AGClose returns kAGErrInvalidRefNum if the guide file was opened by another application.
  92.             if ( ::AGClose( &mGuideFileRefNum ) != noErr ) break;
  93.             err = ::AGQuit();
  94.             break;
  95.  
  96.         default:
  97.             break;
  98.     }
  99. }
  100.  
  101.  
  102. // ==================================================
  103. //    Member functions
  104. // ==================================================
  105.  
  106. // --------------------------------------------------
  107. //        • AppleGuideIsPresent
  108. // --------------------------------------------------
  109. // Check if Apple Guide is installed or not.
  110.  
  111. Boolean
  112. CAppleGuideFile::AppleGuideIsPresent( void )
  113. {
  114.     //check if the Apple Guide extension is installed.
  115.     return UEnvironment::HasGestaltAttribute( gestaltHelpMgrAttr, gestaltAppleGuidePresent );
  116. }
  117.  
  118.  
  119. // --------------------------------------------------
  120. //        • Open
  121. // --------------------------------------------------
  122. // Open the specified guide file located in the application's folder.
  123.  
  124. AGErr
  125. CAppleGuideFile::Open( void )
  126. {
  127.     AGErr    err;
  128.  
  129.     if ( ! AppleGuideIsPresent() ) {
  130.         err = kAGErrAppleGuideNotAvailable;
  131.         UErrorMessage::NofityIfOSErr( err, false );
  132.         return err;
  133.     }
  134.  
  135.     // Check if Apple Guide file is already open or not.
  136.     // kAGIsActive(open), kAGIsSleeping(running but not open), kAGIsNotRunning(not running)
  137.     AGStatus        agStatus;
  138.     agStatus = ::AGGetStatus();
  139.     if ( agStatus == kAGIsActive ) return kAGErrDatabaseOpen;
  140.  
  141.     // If a particular guide file was not found in the same folder of application, then returns err.
  142.     // If guide file was found, the mGuideFileFSSpec will be set to its file spec.
  143. //    err = FindGuideFile( mGuideFilename );
  144.     err = FindGuideFile( mGuideCreatorType );
  145.  
  146.     if ( err != noErr ) {
  147.         err = kAGErrDatabaseNotAvailable;
  148.         UErrorMessage::NofityIfOSErr( err, false );
  149.         return err;
  150.     }
  151.  
  152.     // Open a guide file and show a Full Access window.
  153.     AGRefNum        agRefNum;
  154.     err = ::AGOpen( &mGuideFileFSSpec /*kAGDefault*/, 0, NULL, &agRefNum );
  155.     if ( err != noErr ) return err;
  156.  
  157.     mGuideFileRefNum = agRefNum;
  158.  
  159.     return err;
  160. }
  161.  
  162.  
  163. // --------------------------------------------------
  164. //        • FindGuideFile
  165. // --------------------------------------------------
  166. // Find a default guide file in the same folder where the application is located.
  167. // The file type of default guide file is kAGFileMain('poco').
  168.  
  169. OSErr
  170. CAppleGuideFile::FindGuideFile(
  171.     ConstStr255Param    inFileName )
  172. {
  173.     OSErr    err;
  174.  
  175.     FSSpec    theApplFSSPec;
  176.     err = UFileTools::GetCurrentApplicationFile( theApplFSSPec );
  177.     if ( err != noErr ) return err;
  178.  
  179.     Boolean    found = false;
  180.     for ( long index = 1; ! found ; index++ ) {
  181.  
  182.         FSSpec        theFSSpec;
  183.         EFileType        theFileType;
  184.         err = UFileTools::SearchDirectory( index, theApplFSSPec.vRefNum, theApplFSSPec.parID,
  185.                             theFSSpec, theFileType );
  186.         if ( err != noErr ) break;
  187.  
  188.         // If it is a directory (folder or volume), then skip to next item.
  189.         if ( theFileType != fileType_File ) continue;
  190.  
  191.         // Now we get a plain file, and check its file name if inFileName is not NULL.
  192.         if ( inFileName[0] > 0 &&
  193.         //    ::CompareString( theFSSpec.name, inFileName, NULL ) != 0 ) {
  194.             ! ::EqualString( theFSSpec.name, inFileName, false, false ) ) {
  195.             continue;
  196.         }
  197.  
  198.         // Now we get a plain file, and check its file type.
  199.         FInfo        theFinderInfo;
  200.         err = ::FSpGetFInfo( &theFSSpec, &theFinderInfo );
  201.         if ( err != noErr ) break;
  202.  
  203.         if ( theFinderInfo.fdType == kAGFileMain ) {
  204.             // Now we have got a guide file, whose file type is a kAGFileMain('poco').
  205.             mGotGuideFile = true;
  206.             mGuideFileFSSpec = theFSSpec;
  207.             err = noErr;
  208.             found = true;
  209.         }
  210.     }
  211.  
  212.     return err;
  213. }
  214.  
  215.  
  216. OSErr
  217. CAppleGuideFile::FindGuideFile(
  218.     OSType        inAppCreatorType )
  219. {
  220.     OSErr    err;
  221.  
  222.     FSSpec    theApplFSSPec;
  223.     err = UFileTools::GetCurrentApplicationFile( theApplFSSPec );
  224.     if ( err != noErr ) return err;
  225.  
  226. //    long        count = AGFileGetDBCount( theApplFSSPec.vRefNum, theApplFSSPec.parID,
  227. //                                kAGFileDBTypeAny, false );
  228.     Boolean    found = false;
  229.     for ( long index = 1; ! found /*&& index <= count*/; index++ ) {
  230.  
  231.         FSSpec    theFSSpec;
  232.         err = AGFileGetIndDB( theApplFSSPec.vRefNum, theApplFSSPec.parID,
  233.                         kAGFileDBTypeAny, false, index, &theFSSpec );
  234.         if ( err != noErr ) break;
  235.  
  236.         // Check if the creator type of the guide file is a specified one.
  237.         OSType    theCreatorType;
  238.         err = AGFileGetHelpMenuAppCreator( &theFSSpec, &theCreatorType );
  239.         if ( err != noErr ) continue;
  240.  
  241.         if ( inAppCreatorType != kOSTypeNil &&
  242.             inAppCreatorType != theCreatorType ) continue;
  243.  
  244.         // The found guide file is a desired one.
  245.         mGotGuideFile = true;
  246.         mGuideFileFSSpec = theFSSpec;
  247.         err = noErr;
  248.         found = true;
  249.     }
  250.  
  251.     return err;
  252. }
  253.  
  254.  
  255. #ifdef COMMENT
  256. OSErr
  257. CAppleGuideFile::FindGuideFile(
  258.     ConstStringPtr        inFileName )
  259. {
  260. #pragma unused ( inFileName )
  261.  
  262.     // Deactivate the desktop.
  263.     UDesktop::Deactivate();
  264.  
  265.     // Browse for a document.
  266.     SFTypeList            theTypeList = {kAGFileMain, kAGFileMixin};    // 'poco', 'mixn'
  267.     StandardFileReply    theReply;
  268.     ::StandardGetFile( NULL, 2, theTypeList, &theReply );
  269.     if (! theReply.sfGood ) return fnfErr;
  270.  
  271.     mGuideFileFSSpec = theReply.sfFile;
  272.  
  273.     // Activate the desktop.
  274.     UDesktop::Activate();
  275.  
  276.     return noErr;
  277. }
  278. #endif // COMMENT
  279.  
  280.  
  281. // end of program
  282.